package com.t309.tzy.controller;
import com.t309.tzy.common.TSysUserFormatter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 */
@ControllerAdvice //开启全局异常处理
@Controller
public class ExceptionController {
    //@PostConstruct构造之后; spring启动完后会执行所有配置被这个@PostConstruct注解标记的方法
    /*@PostConstruct
    public void init(){
        System.out.println("ExceptionController ==>init userService=============");
    }*/
    /*@ExceptionHandler(ArithmeticException.class)
    public String arithmeticException(ArithmeticException exception){
        System.out.println("抛异常了:" + exception);
        exception.printStackTrace();
        return "error";
    }*/


    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public String handleExeption(Exception ex) {
        System.out.println("抛异常了:" + ex);
        ex.printStackTrace();
        String resultStr = "异常：ArithmeticException";
        return resultStr;
    }
//————————————————
//    版权声明：本文为CSDN博主「lkforce」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。
//    原文链接：https://blog.csdn.net/lkforce/article/details/98494922

    @ExceptionHandler
    public String exception(Exception ex){
        System.out.println("抛异常了:" + ex);
        ex.printStackTrace();
        return "error";
    }

    public void bind(WebDataBinder dataBinder){
        dataBinder.addCustomFormatter(new TSysUserFormatter());
    }

}
